using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System;
using System.Globalization;

namespace EvoBoost.Core
{
	[Serializable]
	public class TreeRegion<T>
	{
		private readonly List<SplitRule<T>> splitRules;
		
		private readonly ReadOnlyCollection<SplitRule<T>> splitRulesReadOnly;
		
		internal TreeRegion(IEnumerable<SplitRule<T>> splitRules, double outcome)
		{
			Debug.Assert(splitRules != null);

			this.Outcome = outcome;
			this.splitRules = new List<SplitRule<T>>(splitRules);
			this.splitRulesReadOnly = new ReadOnlyCollection<SplitRule<T>>(this.splitRules);
		}

		public ReadOnlyCollection<SplitRule<T>> SplitRules
		{
			get { return this.splitRulesReadOnly; }
		}

		public double Outcome { get; private set; }

		public bool CoversObject(T properties)
		{
			foreach (SplitRule<T> rule in this.splitRules)
			{
				if (!rule.CoversObject(properties))
					return false;
			}

			return true;
		}

		public override string ToString()
		{
			string result = "[";
			foreach (SplitRule<T> rule in splitRules)
				result += rule + "; ";
			if (result.Length > 1)
				result = result.Substring(0, result.Length - 2);	// Remove last semicolon and space
			result += "]";
			result = String.Format(CultureInfo.CurrentCulture, "Outcome {0} for {1}", this.Outcome, result);

			return result;
		}
	}
}